
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __WEEK_DATE_H__
#define __WEEK_DATE_H__

namespace OHOS {

typedef enum {
    WEEK_SUNDAY = 0,
    WEEK_MONDAY,
    WEEK_TUESDAY,
    WEEK_WEDNESDAY,
    WEEK_THURSDAY,
    WEEK_FRIDAY,
    WEEK_SATURDAY,

    WEEK_END
} WEEK_DATE;

static const char *weekDate_[WEEK_END] = {
    "星期天",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六"
};

static const char *weekDateEn_[WEEK_END] = {
    "SUN",
    "MON",
    "TUE",
    "WED",
    "THU",
    "FRI",
    "SAT"
};
class WeekDate {
public:
    static constexpr int LANGUAGE_CHI = 0;
    static constexpr int LANGUAGE_ENG = 1;

    WeekDate() = default;
    ~WeekDate(){};

    static const char *GetWeek(int idx, int language)
    {
        if (idx < WEEK_SUNDAY || idx >= WEEK_END) {
            return nullptr;
        }

        if (language == LANGUAGE_CHI) {
            return weekDate_[idx];
        } else {
            return weekDateEn_[idx];
        }
    }

    static const char *GetWeekByYear(int year, int month, int day, int language)
    {
        const int16_t months = 12;
        const int16_t january = 1;
        const int16_t february = 2;
        const int16_t oneHundred = 100;
        const int16_t fourHundred = 400;
        int iWeekDay;

        if (month == january || month == february) {
            month += months;
            year--;
        }

        // 1 : MONDAY_LAUNCHER, 2 : TUESDAY_LAUNCHER, 3 : WEDNESDAY_LAUNCHER, 4 : , 5 : ect
        iWeekDay = (day + 1 + 2 * month + 3 * (month + 1) / 5 + year +
                    year / 4 - year / oneHundred + year / fourHundred) % WEEK_END;

        if (language == LANGUAGE_CHI) {
            return weekDate_[iWeekDay];
        } else {
            return weekDateEn_[iWeekDay];
        }
    }
};
}
#endif  /* __WEEK_DATE_H__ */
